package com.yxcz.controller;

import java.util.ArrayList;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxcz.model.AddRoleForm;
import com.yxcz.model.JsonMenu;
import com.yxcz.model.Menu;
import com.yxcz.model.Role;
import com.yxcz.model.UpdateRoleForm;
import com.yxcz.model.User;
import com.yxcz.service.MenuService;
import com.yxcz.service.RoleService;
import com.yxcz.service.UserService;

@Controller
@RequestMapping("/Role")
public class RoleController {

	@Resource
	private RoleService roleService;
	@Resource
	private UserService userService;
	@Resource
	private MenuService menuService;

	/**
	 * 权限组管理页面
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "index")
	public ModelAndView index(ModelAndView modelAndView) {

		modelAndView.setViewName("Manager/Role");
		modelAndView.addObject("roles", roleService.list());
		return modelAndView;
	}

	/**
	 * ajax获取权限组信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getRole")
	@ResponseBody
	public Role getRole(Integer id) {

		if (id == null)
			return null;

		Role role = roleService.getRole(id);

		return role;
	}

	/**
	 * 删除权限组
	 * 
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "deleteById")
	public ModelAndView deleteById(Integer id, ModelAndView modelAndView) {
		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
		}

		else {
			roleService.deleteById(id);
			modelAndView.addObject("msg", "删除");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}

	/**
	 * 更新权限组
	 * 
	 * @param form
	 * @param bindingResult
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "update")
	public ModelAndView update(@Validated UpdateRoleForm form, BindingResult bindingResult, ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			roleService.update(form);
			modelAndView.addObject("msg", "修改");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}

	/**
	 * 添加权限组
	 * 
	 * @param form
	 * @param bindingResult
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "add")
	public ModelAndView add(@Validated AddRoleForm form, BindingResult bindingResult, ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			try {
				roleService.add(form);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				modelAndView.setViewName("redirect:/Common/500Page");
				return modelAndView;
			}
			modelAndView.addObject("msg", "增加");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}

	/**
	 * 分配权限组页面
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "user")
	public ModelAndView user(String keyword, ModelAndView modelAndView) {

		modelAndView.setViewName("Manager/Role_user");
		if (keyword == null)
			modelAndView.addObject("users", userService.list());
		else
			modelAndView.addObject("users", userService.listByKeyWord(keyword));
		modelAndView.addObject("roles", roleService.list());
		return modelAndView;
	}

	/**
	 * ajax根据用户获取权限组
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getRolesByUserId")
	@ResponseBody
	public Set<Role> getRolesByUserId(Integer id) {

		if (id == null)
			return null;

		Set<Role> roles = roleService.getRolesByUserId(id);

		return roles;
	}

	/**
	 * 分配权限组
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "setRole")
	public ModelAndView setRole(Integer user_id[], Integer role_list[], ModelAndView modelAndView) {

		if (user_id == null || role_list == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		for (Integer uid : user_id) {
			if (roleService.setRole(uid, role_list) == false) {
				modelAndView.setViewName("redirect:/Common/500Page");
				return modelAndView;
			}
		}

		modelAndView.addObject("msg", "保存");
		modelAndView.setViewName("Common/success");
		return modelAndView;
	}

	/**
	 * 测试JSON函数
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "getJsonTree")
	@ResponseBody
	public ArrayList<JsonMenu> getJsonTree() {
		ArrayList<JsonMenu> menutree = menuService.list();
		return menutree;
	}

	/**
	 * 分配权限页面
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "node")
	public ModelAndView node(String keyword, ModelAndView modelAndView) {

		modelAndView.setViewName("Manager/Role_node");
		
		//传送角色列表到前台
		modelAndView.addObject("roles", roleService.list());
		
		//传送菜单树JSON到前台
		ArrayList<JsonMenu> menus = menuService.list();
		ObjectMapper objectMapper = new ObjectMapper();  
	    String userMapJson;
		try {
			userMapJson = objectMapper.writeValueAsString(menus);
			modelAndView.addObject("menus", userMapJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	

		
		return modelAndView;
	}

	/**
	 * 根据角色获取权限返回JSON
	 * 
	 *
	 * @return
	 */
	@RequestMapping(value = "getMenusByRole")
	@ResponseBody
	public Set<Menu> getMenusByRole(int id) {

		return roleService.getRole(id).getMenus();
	}

	/**
	 * 分配权限
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "setMenu")
	public ModelAndView setMenu(Integer role_id, Integer menu_list[], ModelAndView modelAndView) {

		if (role_id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		
		if(menu_list == null)
			menu_list=new Integer[0];

		if (menuService.setMenu(role_id, menu_list) == false) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		modelAndView.addObject("msg", "保存");
		modelAndView.setViewName("Common/success");
		return modelAndView;
	}

}
